import { BaseMgr } from "../base/BaseMgr";
import BaseCtrl from "../mvc/BaseCtrl";

export class ControllerMgr extends BaseMgr {
    public static get inst() {
        return this.getInstance()
    }

    controllers: { [module: number|string]: BaseCtrl } = {};

    get<T extends BaseCtrl>(module: number|string) {
        let ctrl = this.controllers[module];
        if (!ctrl) {
            console.warn("[ControllerMgr] Ctrl模块未注册", module)
        }
        return ctrl as T;
    }

    registerController<T extends BaseCtrl>(module: number|string, ctrlClass: new () => T) {
        const ctrl = new ctrlClass();
        this.controllers[module] = ctrl;
        return ctrl;
    }

    destroy(): void {
        for (let i in this.controllers) {
            const ctrl = this.controllers[i];
            ctrl.onDestroy();
        }
        this.controllers = {};
    }
}